package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.annotation.AdminLogAnnotation;
import com.market.bean.MarketAdmin;
import com.market.bean.MarketAdminExample;
import com.market.bean.bo.admin.AdminCreateBo;
import com.market.bean.bo.admin.AdminDeleteBo;
import com.market.bean.bo.admin.AdminUpdateBo;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminCreateVo;
import com.market.bean.vo.admin.AdminListEntity;
import com.market.bean.vo.admin.AdminUpdateVo;
import com.market.mapper.MarketAdminMapper;
import com.market.service.admin.AdminAdminService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class AdminAdminServiceImpl implements AdminAdminService {
    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    public CommonData<AdminListEntity> list(BasePageInfo basePageInfo, String username) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        // 如果不为空，则拼接条件
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        // 根据sort做order排序
        marketAdminExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());

        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        List<AdminListEntity> adminListEntities = marketAdmins.stream()
                .map(result -> new AdminListEntity(result.getId(), result.getUsername(), result.getAvatar(), result.getRoleIds()))
                .collect(Collectors.toList());

        PageInfo<AdminListEntity> pageInfo = new PageInfo<>(adminListEntities);
        return CommonData.data(pageInfo);
    }

    @AdminLogAnnotation
    @Override
    public AdminCreateVo create(AdminCreateBo adminCreateBo) {
        MarketAdmin marketAdmin = new MarketAdmin();
        marketAdmin.setUsername(adminCreateBo.getUsername());
        marketAdmin.setPassword(adminCreateBo.getPassword());
        // marketAdmin.setLastLoginIp();
        // marketAdmin.setLastLoginTime();
        marketAdmin.setAvatar(adminCreateBo.getAvatar());
        marketAdmin.setRoleIds(adminCreateBo.getRoleIds());
        marketAdmin.setAddTime(new Date());
        marketAdmin.setUpdateTime(new Date());
        marketAdmin.setDeleted(false);
        marketAdminMapper.insert(marketAdmin);

        MarketAdmin selectByPrimaryKey = marketAdminMapper.selectByPrimaryKey(marketAdmin.getId());
        AdminCreateVo adminCreateVo = new AdminCreateVo();
        adminCreateVo.setId(selectByPrimaryKey.getId());
        adminCreateVo.setUsername(selectByPrimaryKey.getUsername());
        adminCreateVo.setPassword(selectByPrimaryKey.getPassword());
        adminCreateVo.setAvatar(selectByPrimaryKey.getAvatar());
        adminCreateVo.setRoleIds(selectByPrimaryKey.getRoleIds());
        adminCreateVo.setAddTime(selectByPrimaryKey.getAddTime());
        adminCreateVo.setUpdateTime(selectByPrimaryKey.getUpdateTime());
        return adminCreateVo;
    }

    @AdminLogAnnotation
    @Override
    public AdminUpdateVo update(AdminUpdateBo adminUpdateBo) {
        MarketAdmin marketAdmin = marketAdminMapper.selectByPrimaryKey(adminUpdateBo.getId());
        marketAdmin.setUsername(adminUpdateBo.getUsername());
        marketAdmin.setPassword(adminUpdateBo.getPassword());
        marketAdmin.setAvatar(adminUpdateBo.getAvatar());
        marketAdmin.setRoleIds(adminUpdateBo.getRoleIds());
        marketAdmin.setUpdateTime(new Date());
        marketAdminMapper.updateByPrimaryKey(marketAdmin);

        AdminUpdateVo adminUpdateVo = new AdminUpdateVo();
        adminUpdateVo.setId(marketAdmin.getId());
        adminUpdateVo.setUsername(marketAdmin.getUsername());
        adminUpdateVo.setUpdateTime(marketAdmin.getUpdateTime());
        adminUpdateVo.setAvatar(marketAdmin.getAvatar());
        adminUpdateVo.setRoleIds(marketAdmin.getRoleIds());
        return adminUpdateVo;
    }

    @AdminLogAnnotation
    @Override
    public MarketAdmin delete(AdminDeleteBo adminDeleteBo) {
        MarketAdmin marketAdmin = marketAdminMapper.selectByPrimaryKey(adminDeleteBo.getId());
        marketAdmin.setDeleted(true);
        marketAdmin.setUpdateTime(new Date());
        marketAdminMapper.updateByPrimaryKey(marketAdmin);
        return marketAdmin;
    }

    @Override
    public List<MarketAdmin> getAdmins() {
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> list = marketAdminMapper.selectByExample(marketAdminExample);
        return list;
    }

    @Override
    public void password(MarketAdmin admin) {
        marketAdminMapper.updateByPrimaryKeySelective(admin);
    }


}
